package com.system.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.service.CourseService;
import com.system.service.ScoreService;
import com.system.service.UserService;
import com.system.util.ReflectUtil;

import net.sf.json.JSONObject;

public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AdminServlet(){super();}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("content-type", "text/html; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*"); 
		ReflectUtil.invokeMethods(this.getClass(), request, response);
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public void lookoutScores(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String clazz=request.getParameter("stuclass");
		String major=request.getParameter("major");
		ScoreService service=new ScoreService();
		JSONObject json=service.checkoutQueryScores(clazz, major);
		response.getWriter().write(json.toString());
	}
	/**
	 * 为查询增加分页功能
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryScoresbyPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String clazz=request.getParameter("stuclass");
		String major=request.getParameter("major");
		Integer pageNum=Integer.parseInt(request.getParameter("pageNum"));
		Integer amount=Integer.parseInt(request.getParameter("amount"));
		ScoreService service=new ScoreService();
		JSONObject json=service.checkoutQueryScoresbyPage(clazz, major, pageNum, amount);
		response.getWriter().write(json.toString());
	}
	/**
	 * 列出所有课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void listCourse(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CourseService service=new CourseService();
		JSONObject json=service.checkoutListCourses();
		response.getWriter().write(json.toString());
	}
	/**
	 * 单个删除用户
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void deleteUser(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String username=request.getParameter("username");
		UserService service=new UserService();
		JSONObject json=service.checkoutdeleteUser(username);
		response.getWriter().write(json.toString());
	}
	/**
	 * 查询教师所教的课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryCourse(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String teanum=request.getParameter("teanum");
		CourseService service=new CourseService();
		JSONObject json=service.checkoutqueryCoursebyTeanum(teanum);
		response.getWriter().write(json.toString());
	}
}
